package club.cearnach.web.controller.movie;

import club.cearnach.entity.News;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.INewsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 阮胜
 * @date 2018/5/22 9:40
 */
@Controller
@Slf4j
@RequestMapping("/movie/news")
@Api(tags = "电影新闻控制层")
public class NewsController {
    private final INewsService newsService;

    public NewsController(INewsService newsService) {
        this.newsService = newsService;
    }

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("newsCount", newsService.count());
        return "movie/movie-news-list";
    }

    @GetMapping("/list/fragment")
    public String listFragment(PageBean pageBean, Model model) {
        Page<News> newsPage = newsService.findAllOrderByCreateDateDesc(pageBean);
        model.addAttribute("totalPage", newsPage.getTotalPages());
        model.addAttribute("currentPage", pageBean.getPage());
        return "movie/movie-news-list-fragment";
    }
}